package com.example.sigin.com.sign;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;

/**
 * Created by 王子丰 on 2016/10/21.
 */

public class TextDialog extends Dialog {
    Context context;
    private WindowManager.LayoutParams p;

    public TextDialog(Context context) {
        super(context);
        this.context=context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        p=getWindow().getAttributes();
        WindowManager windowManager= (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        p.height=windowManager.getDefaultDisplay().getHeight();
        p.width=windowManager.getDefaultDisplay().getWidth();
        getWindow().setAttributes(p);
        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.tablet_view);
        MyPaintView td=new MyPaintView(context);
        frameLayout.addView(td);
        td.requestFocus();

    }
    class MyPaintView extends View {
        Paint paint;
        Canvas cacheCanvas;
        Bitmap cacheBitmap;
        Path path;
        Context context;

        public MyPaintView(Context context) {
            super(context);
            this.context=context;
            innit();
        }

        private void innit() {
        paint=new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);
            paint.setStrokeWidth(3);
            paint.setStyle(Paint.Style.STROKE);
            path=new Path();
            cacheBitmap=Bitmap.createBitmap(p.width,p.height, Bitmap.Config.ARGB_8888);
            cacheCanvas=new Canvas(cacheBitmap);
            cacheCanvas.drawColor(Color.WHITE);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawBitmap(cacheBitmap,0,0,null);
            canvas.drawPath(path,paint);
        }

        @Override
        protected void onSizeChanged(int w, int h, int oldw, int oldh) {
            super.onSizeChanged(w, h, oldw, oldh);
        }
        private float cur_x,cur_y;
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    cur_x=x;
                    cur_y=y;
                    path.moveTo(cur_x,cur_y);
                    break;
                case MotionEvent.ACTION_MOVE:
                    path.quadTo(cur_x,cur_y,x,y);
                    cur_x=x;
                    cur_y=y;
                    break;
                case MotionEvent.ACTION_UP:
                    cacheCanvas.drawPath(path,paint);
                    path.reset();
                    break;
            }
            invalidate();
            return true;
        }
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }
}
